package com.kuaimacode.kframework.admin.services.weather;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.weather.WeatherCfgMapper;
import com.kuaimacode.kframework.mybatis.models.sys.Dict;
import com.kuaimacode.kframework.mybatis.models.weather.WeatherCfg;
import com.kuaimacode.kframework.service.DictService;
import com.kuaimacode.kframework.util.DictUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author anzy
 * @date 2019/04/07
 */
@Service
public class  WeatherCfgService extends ServiceImpl<WeatherCfgMapper, WeatherCfg> {

		@Autowired
		private DictService dictService;

    	/**
    	 *  分页查找所有的数据
    	 * @param pageNum 当前页码，从1开始
    	 * @param pageSize  一页显示多少行
    	 * @return
    	 */
    	public PageInfo<WeatherCfg> findAll(int pageNum, int pageSize) {
    		PageHelper.startPage(pageNum, pageSize);
    		QueryWrapper queryWrapper = new QueryWrapper();
    		queryWrapper.eq("agent_id","000000");
    		queryWrapper.orderByAsc("weather_id");
    		List<WeatherCfg> datas = baseMapper.selectList(queryWrapper);
    		return new PageInfo<WeatherCfg>(datas);
    	}

		@Transactional
    	public String saveWeatherCfg(WeatherCfg weatherCfg){

			try {
				String weatherName = weatherCfg.getWeatherName();
				if("".equals(weatherName)) return "特殊天气名称不能为空";
				//判断是否已经存在
				boolean isExists = dictService.checkExist(Constant.WEATHER_DICT_NAME,weatherName.trim(),0L);
				if(isExists) return "特殊天气名称已经存在";
				int dict_code = dictService.getMaxCode(Constant.WEATHER_DICT_NAME)+1;
				Dict dict = new Dict();
				dict.setLevel(2);
				dict.setType((byte)2);
				dict.setTitle("特殊天气");
				dict.setCode(dict_code+"");
				dict.setValue(weatherName.trim());
				dict.setCreateAt(new Date());
				dict.setCreateBy(SessionUtil.getUserId());
				dict.setName(Constant.WEATHER_DICT_NAME);
				//获取父节点信息
				Dict pdict = dictService.getName(Constant.WEATHER_DICT_NAME);
				dict.setParentId(pdict.getId());
				dictService.save(dict);

				//清除缓存
				DictUtil.clearCache(Constant.WEATHER_DICT_NAME);

				//保存天气配置表
				weatherCfg.setWeatherId(dict_code);
				weatherCfg.setAgentId("000000");
				weatherCfg.setCreateAt(new Date());
				weatherCfg.setCreateBy(SessionUtil.getUserId());
				baseMapper.insert(weatherCfg);
				return "success";
			}catch (Exception e){
				e.printStackTrace();
				return e.getMessage();
			}

		}

		@Transactional
		public String updateWeatherCfg(WeatherCfg weatherCfg){

    		try{
				String weatherName = weatherCfg.getWeatherName();
				//查询该字典项
				Dict dict = dictService.getDictByNameAndCode(Constant.WEATHER_DICT_NAME,weatherCfg.getWeatherId()+"");

				if(weatherName==null ||"".equals(weatherName.trim())) return "特殊天气不能为空";
				if(dictService.checkExist(Constant.WEATHER_DICT_NAME,weatherName,dict.getId())) return "该特殊天气名称已经存在";

				//更新字典表
				dict.setValue(weatherName);
				dictService.updateById(dict);
				//更新服务表
				weatherCfg.setUpdateAt(new Date());
				weatherCfg.setUpdateBy(SessionUtil.getUserId());
				baseMapper.updateById(weatherCfg);
				//清除缓存
				DictUtil.clearCache(Constant.WEATHER_DICT_NAME);

    			return "success";
			}catch (Exception e){

    			e.printStackTrace();
    			return e.getMessage();
			}



		}


}

